package com.zy.fee.roadpark.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.zy.fee.roadpark.model.M_Escape;
import com.zygk.library.adapter.BaseListAdapter;
import com.zygk.library.util.DensityUtil;
import com.zygk.library.util.ListUtils;
import com.zygk.library.view.FixedListView;
import com.zy.fee.roadpark.R;
import java.util.List;

/**
 * Created by Wikison on 2019/3/2.
 */
public class EscapeListAdapter extends BaseListAdapter<M_Escape> {
  EscapeItemAdapter escapeItemAdapter;
  private OnPrintClickListener onPrintClickListener;

  public EscapeListAdapter(Context context, List<M_Escape> list) {
    super(context, list);
  }

  public void setData(List<M_Escape> list, boolean isLoadMore) {
    if (!isLoadMore) {
      clearAll();
    }
    addALL(list);
    notifyDataSetChanged();
  }

  public void setData(List<M_Escape> list) {
    clearAll();
    addALL(list);
    notifyDataSetChanged();
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {

      convertView = mInflater.inflate(R.layout.item_pay_back, null, false);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    M_Escape m = getData().get(position);

    holder.tvServiceName.setText("车牌号：" + m.getCarNumber());

    if (!ListUtils.isEmpty(m.getEscapes())) {
      escapeItemAdapter = new EscapeItemAdapter(mContext, m.getEscapes());
      holder.listView.setAdapter(escapeItemAdapter);
      holder.listView.setDividerHeight(DensityUtil.dip2px(mContext, 5));
    }

    if (m.isExpand()) {
      holder.ivArrow.setImageResource(R.mipmap.arrow_up);
      holder.ivPrinter.setVisibility(View.VISIBLE);
      holder.listView.setVisibility(View.VISIBLE);
    } else {
      holder.ivArrow.setImageResource(R.mipmap.arrow_down);
      holder.ivPrinter.setVisibility(View.INVISIBLE);
      holder.listView.setVisibility(View.GONE);
    }

    holder.llService.setOnClickListener(v -> {
      m.setExpand(!m.isExpand());
      notifyDataSetChanged();
    });

    holder.ivPrinter.setOnClickListener(v -> {
      if(onPrintClickListener!=null){
        onPrintClickListener.onItemClick(m);
      }
    });

    return convertView;
  }

  public interface OnPrintClickListener{
    void onItemClick(M_Escape mCarNumber);
  }

  public void setOnPrintClickListener(
      OnPrintClickListener onPrintClickListener) {
    this.onPrintClickListener = onPrintClickListener;
  }

  static class ViewHolder {
    @BindView(R.id.tv_carNumber) TextView tvServiceName;
    @BindView(R.id.iv_arrow) ImageView ivArrow;
    @BindView(R.id.ll_carNumber) LinearLayout llService;
    @BindView(R.id.iv_printer) ImageView ivPrinter;
    @BindView(R.id.listView) FixedListView listView;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
